package com.wk1un2.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wk1un2.entity.Product;
import com.wk1un2.service.ProductService;
import com.wk1un2.util.Result;

/**
 * 商品控制器
 * 
 * @author Administrator
 *
 */
@Controller
public class ProductController {
	@Autowired
	ProductService service;

	@RequestMapping("/Backstage/getAllProduct")
	@ResponseBody
	public Object getAll(@RequestParam("page") int page, @RequestParam("limit") int limit,
			@RequestParam("p_name") String p_name, @RequestParam("p_state") String p_state) {

		if (p_name == null || p_name == "") {
			p_name = null;
		}
		Integer state = null;
		if (p_state != null && p_state != "") {

			state = Integer.parseInt(p_state);
		}

		List<Product> ls = service.getAll((page - 1) * limit, limit, p_name, state);
		Result res = new Result(0, "", service.count(), ls);
		return res;
	}

//	
	@RequestMapping("/Backstage/addProduct")
	@ResponseBody
	public Object add(@RequestParam Map<String, Object> map) {

		System.out.println(map.get("p_name"));
		System.out.println(map.get("p_state"));
		int i = service.add(map);
		Result res = new Result(0, "", i, i);
		return res;
	}

//	薷
	@RequestMapping("/Backstage/updateProduct")
	@ResponseBody
	public Object update(@RequestParam Map<String, Object> map) {
		System.out.println(map.get("p_name"));
		System.out.println(map.get("p_state"));
		int i = service.update(map);
		Result res = new Result(0, "", i, i);
		return res;
	}

//	删
	@RequestMapping("/Backstage/deleteProduct")
	@ResponseBody
	public Object delete(@RequestParam("p_id") int p_id) {
		System.out.println(p_id);
		int i = service.delete(p_id);
		Result res = new Result(0, "", i, i);
		return res;
	}
	//修改状态
	@RequestMapping("/Backstage/updateproductstate")
	@ResponseBody
	
	public Result updateproductstate(Integer p_id, Integer p_state) {
		service.updateproductstate(p_id, p_state);
		return Result.ok("1");
	}
}